﻿namespace AnyOffice.WebControls
{
    using System;
    using System.Data;
    using System.Reflection;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class HFPanel : Panel
    {
        public static bool BindForm(Panel Panel1, DataView dv)
        {
            if (dv.Count > 0)
            {
                foreach (Control control in Panel1.Controls)
                {
                    string propertyName;
                    HFCalendar calendar;
                    HFDropDownList list;
                    HFCheckBox box2;
                    HFRadioButton button;
                    HFRadioButtonList list2;
                    string name = control.GetType().Name;
                    if (name != null)
                    {
                        if (!(name == "HFTextBox"))
                        {
                            if (name == "HFCalendar")
                            {
                                goto Label_0109;
                            }
                            if (name == "HFDropDownList")
                            {
                                goto Label_015E;
                            }
                            if (name == "HFCheckBox")
                            {
                                goto Label_01B6;
                            }
                            if (name == "HFRadioButton")
                            {
                                goto Label_0267;
                            }
                            if (name == "HFRadioButtonList")
                            {
                                goto Label_02E5;
                            }
                        }
                        else
                        {
                            HFTextBox box = (HFTextBox) control;
                            propertyName = box.PropertyName;
                            if ((propertyName != "") && (dv[0][propertyName] != null))
                            {
                                box.Text = dv[0][propertyName].ToString();
                            }
                        }
                    }
                    goto Label_033A;
                Label_0109:
                    calendar = (HFCalendar) control;
                    propertyName = calendar.PropertyName;
                    if ((propertyName != "") && (dv[0][propertyName] != null))
                    {
                        calendar.Text = dv[0][propertyName].ToString();
                    }
                    goto Label_033A;
                Label_015E:
                    list = (HFDropDownList) control;
                    propertyName = list.PropertyName;
                    if ((propertyName != "") && (dv[0][propertyName] != null))
                    {
                        list.SetValue(dv[0][propertyName].ToString());
                    }
                    goto Label_033A;
                Label_01B6:
                    box2 = (HFCheckBox) control;
                    propertyName = box2.PropertyName;
                    if ((((propertyName != "") && (dv[0][propertyName] != null)) && (dv[0][propertyName].ToString() != "")) && ((dv[0][propertyName].ToString() == "True") || (dv[0][propertyName].ToString() == "1")))
                    {
                        box2.Checked = true;
                    }
                    else
                    {
                        box2.Checked = false;
                    }
                    goto Label_033A;
                Label_0267:
                    button = (HFRadioButton) control;
                    propertyName = button.PropertyName;
                    if ((((propertyName != "") && (dv[0][propertyName] != null)) && (dv[0][propertyName].ToString() != "")) && (int.Parse(dv[0][propertyName].ToString()) > 0))
                    {
                        button.Checked = true;
                    }
                    goto Label_033A;
                Label_02E5:
                    list2 = (HFRadioButtonList) control;
                    propertyName = list2.PropertyName;
                    if ((propertyName != "") && (dv[0][propertyName] != null))
                    {
                        list2.SetValue(dv[0][propertyName].ToString());
                    }
                Label_033A:;
                }
            }
            return true;
        }

        public void BindQueryForm(DataView dv)
        {
            if (dv.Count > 0)
            {
                foreach (Control control in this.Controls)
                {
                    if (control.GetType().Name == "HFLabel")
                    {
                        HFLabel label = (HFLabel) control;
                        string propertyName = label.PropertyName;
                        if ((propertyName != "") && (dv[0][propertyName] != null))
                        {
                            label.Text = dv[0][propertyName].ToString();
                        }
                    }
                }
            }
        }

        private bool CheckHtml(string strText)
        {
            Regex regex = new Regex("<[^>]*>");
            return regex.Match(strText).Success;
        }

        public int CheckLength(string strFieldValue)
        {
            return Convert.ToInt32(Encoding.GetEncoding("GB2312").GetByteCount(strFieldValue));
        }

        public string FormCheck(object Entity)
        {
            string str = "";
            string strAlertLength = "";
            string str3 = "";
            string strAlertType = "";
            foreach (Control control in this.Controls)
            {
                if (!control.Visible)
                {
                    continue;
                }
                switch (control.GetType().Name)
                {
                    case "HFTextBox":
                        if (((HFTextBox) control).PropertyName != "")
                        {
                            str3 = str3 + this.TextBoxCheck(control, Entity, "HF", ref strAlertLength, ref strAlertType);
                        }
                        break;

                    case "HFRadioButtonList":
                    {
                        HFRadioButtonList list = (HFRadioButtonList) control;
                        if ((list.PropertyName != "") && (list.SelectedValue == ""))
                        {
                            str3 = str3 + list.ChineseName + "、";
                        }
                        break;
                    }
                }
            }
            if (str3 != "")
            {
                str3 = "如下内容必须录入：" + str3;
                str3 = str3.Remove(str3.Length - 1, 1);
            }
            if (strAlertLength != "")
            {
                strAlertLength = @"\n如下内容超过数据库字段长度：" + strAlertLength;
                strAlertLength = strAlertLength.Remove(strAlertLength.Length - 1, 1);
            }
            if (strAlertType != "")
            {
                strAlertType = @"\n如下内容数据库类型不匹配：" + strAlertType;
                strAlertType = strAlertType.Remove(strAlertType.Length - 1, 1);
            }
            str = str3 + strAlertLength + strAlertType;
            string str6 = this.FormHtmlCheck();
            if (str6 != "")
            {
                str = str + @"\n" + str6;
            }
            return str;
        }

        public string FormHtmlCheck()
        {
            string str = "";
            foreach (Control control in this.Controls)
            {
                if (!control.Visible)
                {
                    continue;
                }
                string name = control.GetType().Name;
                if ((name != null) && (name == "HFTextBox"))
                {
                    HFTextBox box = (HFTextBox) control;
                    if (((box.PropertyName != "") && (box.PropertyValue != box.Text)) && this.CheckHtml(box.Text))
                    {
                        str = str + box.ChineseName + "、";
                    }
                }
            }
            if (str != "")
            {
                str = "如下控件输入中含有类似html标签的特殊字符：" + str;
                str = str.Remove(str.Length - 1, 1) + @"。\n请重新输入！";
            }
            return str;
        }

        public static void GetForm(Panel Panel1, object Entity)
        {
            foreach (Control control in Panel1.Controls)
            {
                string propertyName;
                HFCheckBox box;
                switch (control.GetType().Name)
                {
                    case "HFTextBox":
                    {
                        HFTextBox box2 = (HFTextBox) control;
                        propertyName = box2.PropertyName;
                        if (propertyName != "")
                        {
                            SetValue(Entity, propertyName, box2.Text.Trim());
                        }
                        goto Label_027C;
                    }
                    case "HFCalendar":
                    {
                        HFCalendar calendar = (HFCalendar) control;
                        propertyName = calendar.PropertyName;
                        if (propertyName != "")
                        {
                            SetValue(Entity, propertyName, calendar.Text.Trim());
                        }
                        goto Label_027C;
                    }
                    case "HFDropDownList":
                    {
                        HFDropDownList list = (HFDropDownList) control;
                        propertyName = list.PropertyName;
                        if ((propertyName != "") && (list.SelectedValue != null))
                        {
                            SetValue(Entity, propertyName, list.SelectedValue);
                        }
                        goto Label_027C;
                    }
                    case "HFCheckBox":
                        box = (HFCheckBox) control;
                        propertyName = box.PropertyName;
                        if (box.Checked)
                        {
                            break;
                        }
                        goto Label_024C;

                    case "HFRadioButton":
                    {
                        HFRadioButton button = (HFRadioButton) control;
                        propertyName = button.PropertyName;
                        if ((propertyName != "") && (button.SelectValue != null))
                        {
                            SetValue(Entity, propertyName, button.SelectValue);
                        }
                        goto Label_027C;
                    }
                    case "HFRadioButtonList":
                    {
                        HFRadioButtonList list2 = (HFRadioButtonList) control;
                        propertyName = list2.PropertyName;
                        if ((propertyName != "") && (list2.SelectedValue != null))
                        {
                            SetValue(Entity, propertyName, list2.SelectedValue);
                        }
                        goto Label_027C;
                    }
                    default:
                        goto Label_027C;
                }
                box.SelectValue = "true";
                goto Label_0258;
            Label_024C:
                box.SelectValue = "false";
            Label_0258:
                if (propertyName != "")
                {
                    SetValue(Entity, propertyName, box.SelectValue);
                }
            Label_027C:;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.CssClass = "dibg";
        }

        public static void ResetForm(Panel Panel1)
        {
            foreach (Control control in Panel1.Controls)
            {
                HFCalendar calendar;
                HFDropDownList list;
                HFCheckBox box2;
                HFRadioButton button;
                HFRadioButtonList list2;
                string name = control.GetType().Name;
                if (name != null)
                {
                    if (!(name == "HFTextBox"))
                    {
                        if (name == "HFCalendar")
                        {
                            goto Label_00DE;
                        }
                        if (name == "HFDropDownList")
                        {
                            goto Label_011E;
                        }
                        if (name == "HFCheckBox")
                        {
                            goto Label_0161;
                        }
                        if (name == "HFRadioButton")
                        {
                            goto Label_019E;
                        }
                        if (name == "HFRadioButtonList")
                        {
                            goto Label_01DB;
                        }
                    }
                    else
                    {
                        HFTextBox box = (HFTextBox) control;
                        if (box.PropertyName != "")
                        {
                            box.Text = "";
                            box.PropertyValue = "";
                        }
                    }
                }
                goto Label_0217;
            Label_00DE:
                calendar = (HFCalendar) control;
                if (calendar.PropertyName != "")
                {
                    calendar.Text = "";
                    calendar.PropertyValue = "";
                }
                goto Label_0217;
            Label_011E:
                list = (HFDropDownList) control;
                if (list.PropertyName != "")
                {
                    list.ClearSelection();
                    list.SelectedIndex = 0;
                    list.PropertyValue = "";
                }
                goto Label_0217;
            Label_0161:
                box2 = (HFCheckBox) control;
                if (box2.PropertyName != "")
                {
                    box2.Checked = false;
                    box2.PropertyValue = "";
                }
                goto Label_0217;
            Label_019E:
                button = (HFRadioButton) control;
                if (button.PropertyName != "")
                {
                    button.Checked = false;
                    button.PropertyValue = "";
                }
                goto Label_0217;
            Label_01DB:
                list2 = (HFRadioButtonList) control;
                if (list2.PropertyName != "")
                {
                    list2.ClearSelection();
                    list2.PropertyValue = "";
                }
            Label_0217:;
            }
        }

        public static void ResetFormPanl(Panel Panel1)
        {
            foreach (Control control in Panel1.Controls)
            {
                HFCalendar calendar;
                HFCheckBox box2;
                HFRadioButton button;
                HFRadioButtonList list;
                string name = control.GetType().Name;
                if (name != null)
                {
                    if (!(name == "HFTextBox"))
                    {
                        if (name == "HFCalendar")
                        {
                            goto Label_00CA;
                        }
                        if (name == "HFCheckBox")
                        {
                            goto Label_010A;
                        }
                        if (name == "HFRadioButton")
                        {
                            goto Label_0143;
                        }
                        if (name == "HFRadioButtonList")
                        {
                            goto Label_0180;
                        }
                    }
                    else
                    {
                        HFTextBox box = (HFTextBox) control;
                        if (box.PropertyName != "")
                        {
                            box.Text = "";
                            box.PropertyValue = "";
                        }
                    }
                }
                goto Label_01BC;
            Label_00CA:
                calendar = (HFCalendar) control;
                if (calendar.PropertyName != "")
                {
                    calendar.Text = "";
                    calendar.PropertyValue = "";
                }
                goto Label_01BC;
            Label_010A:
                box2 = (HFCheckBox) control;
                if (box2.PropertyName != "")
                {
                    box2.Checked = false;
                    box2.PropertyValue = "";
                }
                goto Label_01BC;
            Label_0143:
                button = (HFRadioButton) control;
                if (button.PropertyName != "")
                {
                    button.Checked = false;
                    button.PropertyValue = "";
                }
                goto Label_01BC;
            Label_0180:
                list = (HFRadioButtonList) control;
                if (list.PropertyName != "")
                {
                    list.ClearSelection();
                    list.PropertyValue = "";
                }
            Label_01BC:;
            }
        }

        private static void SetValue(object Entity, string strPropertyName, string strPropertyValue)
        {
            if (strPropertyValue != "")
            {
                PropertyInfo property = Entity.GetType().GetProperty(strPropertyName);
                object obj2 = Convert.ChangeType(strPropertyValue, property.PropertyType);
                property.SetValue(Entity, obj2, null);
            }
        }

        public string ShowJsMessage(string str)
        {
            return ("<script language='javascript'>alert('" + str + "')</script>");
        }

        private string TextBoxCheck(Control ctl, object Entity, string strControlType, ref string strAlertLength, ref string strAlertType)
        {
            string str = "";
            string strFieldValue = "";
            string propertyName = "";
            string chineseName = "";
            if (strControlType == "HF")
            {
                HFTextBox box = (HFTextBox) ctl;
                strFieldValue = box.Text;
                propertyName = box.PropertyName;
                chineseName = box.ChineseName;
            }
            bool flag = true;
            DbType ansiString = DbType.AnsiString;
            int num = 0;
            switch (ansiString)
            {
                case DbType.AnsiString:
                case DbType.String:
                case DbType.AnsiStringFixedLength:
                case DbType.StringFixedLength:
                    if (num < this.CheckLength(strFieldValue))
                    {
                        strAlertLength = strAlertLength + chineseName + "、";
                    }
                    break;

                case DbType.Currency:
                case DbType.Decimal:
                case DbType.Double:
                case DbType.Single:
                case DbType.VarNumeric:
                    try
                    {
                        decimal num2 = Convert.ToDecimal(strFieldValue);
                    }
                    catch
                    {
                        strAlertType = strAlertType + chineseName + "、";
                    }
                    break;

                case DbType.Date:
                case DbType.DateTime:
                case DbType.Time:
                    try
                    {
                        DateTime time = Convert.ToDateTime(strFieldValue);
                    }
                    catch
                    {
                        strAlertType = strAlertType + chineseName + "、";
                    }
                    break;

                case DbType.Int16:
                case DbType.Int32:
                case DbType.Int64:
                    try
                    {
                        int num3 = Convert.ToInt32(strFieldValue);
                    }
                    catch
                    {
                        strAlertType = strAlertType + chineseName + "、";
                    }
                    break;
            }
            if (!(flag || ((strFieldValue.Trim() != "") && (strFieldValue != null))))
            {
                str = str + chineseName + "、";
            }
            return str;
        }
    }
}

